<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/6 0006
 * Time: 17:11
 */
namespace app\modules\content\models;

use yii\db\ActiveRecord;

class Teachers extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%teachers}}';
    }

    public function rules()
    {
        return [
            // username and password are both required
            [['name', 'introduction', '', 'subject'], 'required'],

        ];
    }

    public function getTeachers($str)
    {
        $names = explode(',', $str);
        $data = Teachers::find()->asArray()->orderBy('sort asc')->all();
        static $teachers;
        foreach ($data as $k => $v) {
            foreach ($names as $val) {
                if ($v['name'] == $val) {
                    $teachers[] = $v;
                }
            }
        }
        return $teachers;
    }

    /**
     * @Title  [获取老师列表]
     * @Author Don
     * @Date   2021-04-13
     * @return [type]
     */
    public function getTeacherList($limit)
    {
        // $data = self::find()->asArray()->orderBy('sort asc')->limit($limit)->all();

        $bookin ="102,101,112,115,107";
        $data =self::find()->where("id in ($bookin) order by  field(id,$bookin)")->asArray()->all();
        return $data;
    }

    /**
     * @Title  [取其他老师]
     * @Author Don
     * @Date   2021-04-13
     * @return [type]
     */
    public function getOtherTeacher($tid)
    {
        $where = '1=1';
        if ($tid) {
            $where = 'id !='.$tid;
        }
        $limit = 3;
        $data = \Yii::$app->db->createCommand("select id,name,pic from {{%teachers}} where $where order by sort asc limit $limit")->queryAll();

        return $data;
    }

    /**
     * @Title  [根据条件筛选老师]
     * @Author yoyo
     * @Date   2021-05-19
     */
    public static function getTeacher($seniority,$subject,$page=1,$pageSize=15)
    {
        $offset=($page-1)*$pageSize;
        $where='1=1';
        if($seniority!=false){
            $where.=' and seniority='.$seniority;
        }
        if($subject!=false){
            $where.=' and (';
            $tem=explode(',',$subject);
            foreach($tem as $k=>$v){
                $where.='subject like "%'.$v.'%"';
                if($k<count($tem)-1){
                    $where.=' or ';
                }else{
                    $where.=' )';
                }
            }
        }
        $data = Teachers::find()->asArray()->distinct()->where($where)->offset($offset)->limit($pageSize)->orderby('sort asc,id desc')->all();
        return $data;
    }

    /**
     * [getWapTeacher description]
     * @Author don
     * @Date   2021-11-12
     * @param  [type]     $seniority [description]
     * @param  [type]     $subject   [description]
     * @param  integer    $page      [description]
     * @param  integer    $pageSize  [description]
     * @return [type]                [description]
     */
    public static function getWapTeacher($seniority,$subject,$page=1,$pageSize=10)
    {
        $offset=($page-1)*$pageSize;
        $where='1=1';
        if($seniority!=false){
            $where.=' and seniority='.$seniority;
        }
        if($subject!=false){
            $where.=' and (';
            $tem=explode(',',$subject);
            foreach($tem as $k=>$v){
                $where.='subject like "%'.$v.'%"';
                if($k<count($tem)-1){
                    $where.=' or ';
                }else{
                    $where.=' )';
                }
            }
        }
        $data = Teachers::find()->asArray()->distinct()->where($where)->offset($offset)->limit($pageSize)->orderby('sort asc,id desc')->all();
        $count = Teachers::find()->asArray()->distinct()->where($where)->orderby('sort asc,id desc')->count();

        $totalPage = ceil($count / $pageSize);
        return ['data'=>$data, 'count' => $count, 'totalPage'=>$totalPage];
    }
}